package com.hy.mydesktop.client.component.factory;

import java.util.List;
import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.desktop.client.StartMenu;
import com.extjs.gxt.desktop.client.TaskBar;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Component;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.AbstractComponetModelEnum;
import com.hy.mydesktop.client.component.meta.DesktopMenuItemModelEnum;
import com.hy.mydesktop.client.component.meta.MenuItemModelEnum;
import com.hy.mydesktop.client.component.meta.ShortcutModelEnum;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.ItemOfAssociateToWindow;
import com.hy.mydesktop.shared.persistence.domain.gxt.constant.ComponentMetaDataConstants;
import com.hy.mydesktop.shared.util.log.MyLoggerUtil;

/**
 * 
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2012-2-13；时间：下午05:46:35</li>
 * <li>类型名称：ItemOfAssociateToWindowFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class ItemOfAssociateToWindowFactory {
	
	
	
	public static Component configureBaseComponentId(Component component,ItemOfAssociateToWindow gxtComponentMetaNodeModel){


		//设置基础控件的Id
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID)!=null){
			String id =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.COMPONENT_ID);
			
			//component.setId(id.replace('.', '_'));
			component.setData(ComponentMetaDataConstants.ID,id);
			component.setData(ComponentMetaDataConstants.COMPONENT_ID,id);
		
		}else {
			
			if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID)!=null){
				//设置基础控件的Id
				String id =gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.ID);
				;
				//component.setId(id.replace('.', '_'));
				component.setData(ComponentMetaDataConstants.ID,id);
				component.setData(ComponentMetaDataConstants.COMPONENT_ID,id);
				
			}else {
				MyLoggerUtil.printDebug(ItemOfAssociateToWindowFactory.class, "没有为当前控件"+component.getClass().getName()+"，设置控件ID");
			}
		}
		
		return component;
	}
	/**
	 * 
	 * <ul>
	 * <li>方法含义：</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-9-17；时间：下午上午09:42:03</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param desktop
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	public static Desktop createMenuItem(Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		
		return configureMenuItem(desktop,gxtComponentMetaNodeModel);
		
		
	}
	
	public static MenuItem createMenuItemToDesktop(Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		
		MenuItem menuItem  = createDefaultMenuItem();
		menuItem = AbstractComponetFactory.configureAbstractComponet(menuItem, gxtComponentMetaNodeModel);
		
		//进行个性化配置
		
		//添加子控件
		if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS)!=null){
			 List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
			 if (list.size()>0) {
				 Menu menu=new Menu();
				 for(int i=0;i<list.size();i++)
				 {
					 MenuItem subItem=MenuItemFactory.createItemOfAssociateToWindow(list.get(i));
					 menu.add(subItem);
				 }	
				 menuItem.setSubMenu(menu);
			}
		}
		
		//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
		if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase())!=null) {
			//设置Button的title
			 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			menuItem.setText((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase()));
			
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ID.name().toLowerCase())!=null) {
			
			menuItem.setId((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.ID.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase())!=null) {
			
			menuItem.setTitle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase())!=null) {
			
			menuItem.setIconStyle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase()));
		}
						
		desktop.getTaskBar().getStartMenu().add(menuItem);
				
		 //StartMenu startMenu = taskBar.getStartMenu();
		 //startMenu.add(menuItem);
	
		return menuItem;
		
		
	}
	
	private static Desktop configureMenuItem(final Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		MenuItem menuItem  = createDefaultMenuItem();
	
		//进行个性化配置
		//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
		if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase())!=null) {
			//设置Button的title
			 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			menuItem.setText((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TEXT.name().toLowerCase()));
			
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase())!=null) {
			
			menuItem.setTitle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.TITLE.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase())!=null) {
			
			menuItem.setIconStyle((String)gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.ICON.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(DesktopMenuItemModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
					
			final Map<String,Object> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for ( final String code : appEventCodes.keySet()) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 5://Integer.valueOf("0005")
		            	SelectionListener<MenuEvent> shortcutListener = new SelectionListener<MenuEvent>() {
						      @Override
						      public void componentSelected(MenuEvent ce) {
						    	 //itemSelected(desktop,ce);
						    	  Object data = appEventCodes.get(code);
						    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_MENUITEM_ONCLICK_INIT,data);
								 
					    }};
					    /**
						 * 桌面快捷方式的监听器
						 */
					    menuItem.addSelectionListener(shortcutListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		            case 101://Integer.valueOf("0101")
		            	//shortcutController.addRegisterEventType(AppEvents.WINDOW_ININT);
		                break;
	           		           
		            default:
		                break;
		        }
			}		
		
		}
		
		desktop.getTaskBar().getStartMenu().add(menuItem);
		 //StartMenu startMenu = taskBar.getStartMenu();
		 //startMenu.add(menuItem);
	
		return desktop;
		
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-9-15；时间：下午上午11:05:29</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @return
	 */
	private static MenuItem createDefaultMenuItem() {
		
		
		 MenuItem menuItem = new MenuItem();
		 menuItem.setText("默认的DesktopMenuItem");
		 menuItem.setTitle("title");			 
		 menuItem.setIcon(IconHelper.createStyle("accordion"));
		
		 return menuItem;
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：用来响应桌面的快捷方式的单击事件：模拟视窗系统中，窗口的所以显示效果</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-9-8；时间：下午下午03:32:05</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param ce
	 */
	 private static void itemSelected(Desktop desktop,ComponentEvent ce) {
		    Window w;
		    if (ce instanceof MenuEvent) {
		      MenuEvent me = (MenuEvent) ce;
		      //s1.setData("window", gridWindow);
		      //创建并加载窗口对象
		      w = me.getItem().getData("window");
		    } else {
		    	//s1.setData("window", gridWindow);
			      //创建并加载窗口对象
		      w = ce.getComponent().getData("window");
		    }
		    if (!desktop.getWindows().contains(w)) {
		      desktop.addWindow(w);
		    }
		  //如果w 存在（不为空）且 w是不可见到的
		    if (w != null && !w.isVisible()) {
		    	//w显示
		      w.show();
		    } else {
		    	//w最前面显示
		      w.toFront();
		    }
		  }

	public static void configureChildrenItems(Component component,GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		/*if(gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS)!=null){
			String childrenItemsType=gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS_TYPE);

			if (childrenItemsType==null||childrenItemsType.equals("component")) {
				List<ItemOfAssociateToWindow> list =(List<ItemOfAssociateToWindow>)gxtComponentMetaNodeModel.get(ComponentMetaDataConstants.CHILDREN_ITEMS);
				 if (list.size()>0) {
					 Menu menu=new Menu();
					 for(int i=0;i<list.size();i++)
					 {
						 MenuItem subItem=MenuItemFactory.createItemOfAssociateToWindow(list.get(i));
						 menu.add(subItem);
					 }	
					 menuItem.setSubMenu(menu);
				}
			} else {

				
			}
			
		}*/
		
	}
}
